package com.czkj.hwz.decorate.control;

import android.content.Context;



import com.czkj.hwz.decorate.IMP.HttpDoImp;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Sort;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxm on 2017/10/13.
 */

public class GoodsSortGet {
    private Context mContext;
    private HttpDoImp mHttpDoImp;

    public GoodsSortGet(Context mContext) {
        this.mContext = mContext;
        this.mHttpDoImp= (HttpDoImp) mContext;
    }

    public GoodsSortGet(Context mContext, HttpDoImp mHttpDoImp) {
        this.mContext = mContext;
        this.mHttpDoImp = mHttpDoImp;
    }
    public void getGoodsSort() {
        RequestParams params=new RequestParams(HttpUrl.GOODS_SORT);
        params.addQueryStringParameter("longitude", "119.038657");
        params.addQueryStringParameter("latitude", "33.609279");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject jsonResult= null;
                try {
                    jsonResult = new JSONObject(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                JSONObject areaInfo=null;
                try {
                    areaInfo=jsonResult.getJSONObject("AreaInfo");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JSONArray sortList=null;
                try {
                    sortList=jsonResult.getJSONArray("SortList");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String[] names=null;
                List<Sort> sorts=new ArrayList<Sort>();
                Sort mSort=null;
                for (int i=0;i<sortList.length();i++) {
                    try {
                        JSONObject sort=sortList.getJSONObject(i);
                        mSort=new Sort();
                        mSort.setId(sort.getInt("Id"));
                        mSort.setName(sort.getString("Name"));
                        sorts.add(mSort);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                mHttpDoImp.onSuccess("GoodsSortGet",areaInfo,sorts);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
